///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/mat/cmatfile.h>
#include <matio.h>

namespace buola { namespace mat {

struct CMatFile::SImpl
{
    mat_t *mMatFile;
};
    
CMatFile::CMatFile(const io::CURI &pURI)
    :   mImpl(new SImpl)
{
    mImpl->mMatFile=Mat_Open(pURI.Path().c_str(),MAT_ACC_RDONLY);
    
    if(!mImpl->mMatFile)
        throw XNotFound("can't open matlab file");
}

CMatFile::~CMatFile()
{
    Mat_Close(mImpl->mMatFile);
}
    
CMat_d CMatFile::GetMat(const std::string& pName)
{
    matvar_t *lMatVar=Mat_VarReadInfo(mImpl->mMatFile,const_cast<char*>(pName.c_str()));
    if(lMatVar->rank!=2)
        throw XData("matrix has wrong dimensions in matlab file");
    
    mat::CMat_d lReturn(lMatVar->dims[0],lMatVar->dims[1]);
    msg_info() << lReturn.Rows() << "x" << lReturn.Cols() << "\n";
    Mat_VarReadDataLinear(mImpl->mMatFile,lMatVar,lReturn.Data(),0,1,lMatVar->dims[0]*lMatVar->dims[1]);
    Mat_VarFree(lMatVar);
    
    return lReturn;
}
    
img::CImage_gray32f CMatFile::GetImage(const std::string& pName)
{
    matvar_t *lMatVar=Mat_VarReadInfo(mImpl->mMatFile,const_cast<char*>(pName.c_str()));
    if(lMatVar->rank!=2)
        throw XData("matrix has wrong dimensions in matlab file");
    
    img::CImage_gray32f lReturn(CSize_i(lMatVar->dims[0],lMatVar->dims[1]));
    Mat_VarReadDataLinear(mImpl->mMatFile,lMatVar,lReturn.RawBytes(),0,1,lMatVar->dims[0]*lMatVar->dims[1]);
    Mat_VarFree(lMatVar);
    
    return lReturn;
}

/*namespace mat*/ } /*namespace buola*/ }
